/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.sternly.talkafrica;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;

/*
 * Checks if the voice data for the Talk Africa Engine is present on the
 * sd card.
 */
public class CheckVoiceData extends Activity {

    // The following constants are the same path constants as the ones defined
    // in external/tafrica-data/ 
    private final static String TAFRICA_DATA_PATH = Environment.getExternalStorageDirectory()
    + "/tafrica-data/";
    
    private final static String[] baseDataFiles = {
        "af_dict", "config", "en_dict", "phondata", 
        "phonindex", "phontab","sw_dict", "tn_dict", "mbrola/dummyfile", "mbrola_ph/af1_phtrans", 
        "mbrola_ph/en1_phtrans", "mbrola_ph/us_phtrans", "mbrola_ph/us3_phtrans","soundicons/dummyfile", 
        "voices/af", "voices/sw", "voices/tn","voices/!v/croak", "voices/!v/f1", "voices/!v/f2", 
        "voices/!v/f3", "voices/!v/f4", "voices/!v/m1", "voices/!v/m2", "voices/!v/m3", "voices/!v/m4", 
        "voices/!v/m5","voices/!v/m6", "voices/!v/whisper", "voices/en/en", "voices/en/en-n",
        "voices/en/en-r", "voices/en/en-rp", "voices/en/en-sc", "voices/en/en-wi",
        "voices/en/en-wm", "voices/mb/mb-af1", "voices/mb/mb-af1-en", "voices/mb/mb-en1", 
        "voices/mb/mb-us1", "voices/mb/mb-us2", "voices/mb/mb-us3"
    };
    private final static String[] supportedLanguages = {
        "afr",   "eng", "eng-USA", "eng-GBR", "swa", "tsn"
    };
    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int result = TextToSpeech.Engine.CHECK_VOICE_DATA_PASS;
        Intent returnData = new Intent();
        returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_ROOT_DIRECTORY, TAFRICA_DATA_PATH);
        returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_FILES, baseDataFiles);
        returnData.putExtra(TextToSpeech.Engine.EXTRA_VOICE_DATA_FILES_INFO, baseDataFiles);
       
        ArrayList<String> available = new ArrayList<String>();
        ArrayList<String> unavailable = new ArrayList<String>();

        //  Check each language INDIVIDUALLY
        boolean passedAllChecks = true;

        for (int i = 0; i < baseDataFiles.length; i++) {
            if (!fileExists(baseDataFiles[i])) {
                passedAllChecks = false;
                break;
            }
        }
            if (passedAllChecks) {
                for (int i = 0; i < supportedLanguages.length; i++) {
                    available.add(supportedLanguages[i]);
                }
            } else {
                for (int i = 0; i < supportedLanguages.length; i++) {
                    unavailable.add(supportedLanguages[i]);
                }
            }

            returnData.putStringArrayListExtra("availableVoices", available);
            returnData.putStringArrayListExtra("unavailableVoices", unavailable);
            setResult(result, returnData);
            finish();
        }

        private boolean fileExists(String filename) {
            File tempFile = new File(TAFRICA_DATA_PATH + filename);
            File tempFileSys = new File(TAFRICA_DATA_PATH + filename);
            if ((!tempFile.exists()) && (!tempFileSys.exists())) {
                return false;
            }
            return true;
        }
}